import json

from config import OPERATE_MODEL_CONFIG

if __name__ == '__main__':
    WEB_PORT = OPERATE_MODEL_CONFIG['WEB_PORT']
    print("WEB_PORT:", WEB_PORT)
    entity_sentence = []
    entity_sentence_sub = {"entity_text": "TASK212400000501", "entity_label": "任务编号"}
    entity_sentence.append(entity_sentence_sub)
    entity_sentence_sub2 = {"entity_text": "TASK212400000502", "entity_label": "任务编号"}
    entity_sentence.append(entity_sentence_sub2)
    print(json.dumps(entity_sentence, ensure_ascii=False))

    with open("category/label_config.json", "r") as fileCategory:
        data = json.load(fileCategory)
        match_names = [item['text'] for item in data]
        input = "文本:查询任务编号：TASK212400000501的排产任务表,类型选型:" + json.dumps(match_names, ensure_ascii=False)
        print(input)

    with open("all.jsonl", "r") as file:
        with open("span/label_config.json", "r") as fileSpan:
            data = json.load(fileSpan)
            match_names = [item['text'] for item in data]
            # print(match_names)
        for line in file:
            # 解析每一行的json数据
            data = json.loads(line)
            input_text = data["text"]
            cats = data["cats"]
            entities = data["entities"]
            print("xxxxx" + cats[0])
            entity_sentence = []
            for entity in entities:
                entity_text = input_text[entity[0]:entity[1]]
                entity_name = entity[2]
                for match_name in match_names:
                    if match_name == entity_name:
                        entity_label = entity_name
                        break
                entity_sentence.append({"entity_text": entity_text, "entity_label": entity_label})
            if entity_sentence == "":
                entity_sentence = "没有找到任何实体"

            # print(entity_sentence)
            message = {
                "instruction": "你是一个文本实体识别领域的专家，你需要从给定的句子中提取 " + "; ".join(
                    match_names) + """ 实体. 以 json 格式输出, 如 {"entity_text": "TASK202400000501", "entity_label": "任务编号"} 注意: 1. 输出的每一行都必须是正确的 json 字符串. 2. 找不到任何实体时, 输出"没有找到任何实体".""",
                "input": f"文本类型:{cats[0]};文本:{input_text}",
                "output": json.dumps(entity_sentence, ensure_ascii=False),
            }
            print(json.dumps(message, ensure_ascii=False))
